package com.framework.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.sys.SysAgent;
import com.framework.service.AgentInfoService;
import com.framework.service.AgentService;
import com.framework.service.FlowAccountService;
import com.framework.service.TerminalInfoService;
import com.framework.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 激活任务
 */
@Component("tLAgentActivTask")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class TLAgentActivTask {

    @Autowired
    private AgentService agentService;

    @Autowired
    private AgentInfoService agentInfoService;

    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private FlowAccountService flowAccountService;


    Map<Integer,BigDecimal> dict=new HashMap<>();

    Map<Integer,BigDecimal> dict2=new HashMap<>();

    /**
     * 注册且激活达标
     */
    public void tlAgentActiv(){

        initialDict();

        List<SysAgent> agents = agentService.selectList(new EntityWrapper<SysAgent>().eq("dept_id", 5));
        for (SysAgent agent : agents) {
            List<SysAgent> subAgentList = agentService.selectList(new EntityWrapper<SysAgent>().eq("pid", agent.getAgentId()));

            if(subAgentList.size()<5){
                continue;
            }
            String date= DateUtils.format(DateUtils.addDateMonths(new Date(),-1),"yyyy-MM");

            Long count = subAgentList.stream()
                    .filter(e -> terminalInfoService.queryLastMonthActivCount(e.getAgentId(), date) >= 8)
                    .count();

            BigDecimal reapAmount = getReapAmount(count.intValue(),dict);
            AddReapAmount(agent, reapAmount,9,"激活数量达标奖励");


        }


    }

    /**
     *  通联注册数量达标
     */
    public void tlAgentAccount(){
        log.info("通联注册达标任务开始");
        initialDict2();

        List<SysAgent> agents = agentService.selectList(new EntityWrapper<SysAgent>().eq("dept_id", 5));
        for (SysAgent agent : agents) {
            List<SysAgent> subAgentList = agentService.selectList(new EntityWrapper<SysAgent>().eq("pid", agent.getAgentId()));

            Long count = subAgentList.stream()
                    .filter(e ->terminalInfoService.queryTLCount(e.getAgentId())>= 1)
                    .count();

            BigDecimal reapAmount = getReapAmount(count.intValue(),dict2);

            AddReapAmount(agent, reapAmount,10,"团队数量达标奖励");

            log.info("通联注册达标任务结束");

        }
    }

    private void AddReapAmount(SysAgent agent, BigDecimal reapAmount,int accountType,String remark) {

        FlowAccount account = flowAccountService.selectOne(new EntityWrapper<FlowAccount>().eq("agent_id", agent.getAgentId())
                .eq("amount", reapAmount)
                .eq("support_type", 2)
                .eq("account_type", accountType)

        );

        if(account==null){
            if(reapAmount.compareTo(new BigDecimal("0.00"))==1){

                agentInfoService.addAmount(agent.getAgentId(),reapAmount);
                FlowAccount flowAccount=new FlowAccount();
                flowAccount.setAgentId(agent.getAgentId());
                flowAccount.setCreateTime(new Date());
                flowAccount.setSupportType(2);
                flowAccount.setAccountType(accountType);
                flowAccount.setAmount(reapAmount);
                flowAccount.setProfit(new BigDecimal("0.00"));
                flowAccount.setRemark(remark);
                flowAccountService.insert(flowAccount);

            }
        }

    }


    private BigDecimal getReapAmount(int count, Map<Integer,BigDecimal> map) {
        BigDecimal reapAmount=null;
        Set<Integer> counts = map.keySet();

        List<Integer> collect = counts.stream()
                .sorted(Comparator.comparingInt(Integer::intValue))
                .collect(Collectors.toList());

        for (Integer integer : collect) {
            if(count>=integer){
                reapAmount=map.get(integer);
            }

        }
        if(reapAmount==null){
            return new BigDecimal("0.00");
        }

        return reapAmount;
    }

    /**
     * 初始化注册且激活达标奖励树
     */
    private void initialDict(){
        dict.put(5,new BigDecimal("1900"));
        dict.put(10,new BigDecimal("4500"));
        dict.put(30,new BigDecimal("13800"));
        dict.put(50,new BigDecimal("27800"));
        dict.put(100,new BigDecimal("75800"));
        dict.put(500,new BigDecimal("399800"));
        dict.put(1000,new BigDecimal("999800"));
    }

    /**
     * 初始化注册达标奖励树
     */
    private void initialDict2(){
        dict2.put(100,new BigDecimal("7580"));
        dict2.put(500,new BigDecimal("39980"));
        dict2.put(1000,new BigDecimal("99880"));
        dict2.put(5000,new BigDecimal("399880"));
        dict2.put(10000,new BigDecimal("1399880"));
    }




}
